package com.property.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.property.mapper.BuildingMapper;
import com.property.mapper.ResidentMapper;
import com.property.pojo.Building;
import com.property.pojo.Resident;
import com.property.service.BuildingServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServerImpl implements BuildingServer {
    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private ResidentMapper residentMapper;
    @Override
    public List<Building> findAllBuilding() {
        return buildingMapper.findAllBuilding();
    }

    //查询所有的楼房信息
    @Override
    public String findAllBuildingResident(Integer page) {
        PageHelper.startPage(page,10);
        List<Building> list = buildingMapper.findAllBuilding();
        for(Building build : list){
            //通过rBid查询resident住户
            List<Resident> residentList = residentMapper.findResidentByrBid(build.getbId());
            build.setResidentList(residentList);
        }
        PageInfo pageInfo = new PageInfo(list);
        return JSON.toJSONString(pageInfo);
    }

    //新增更新房屋信息
    @Override
    public String saveBuilding(Building building) {
        String message ="操作成功";
        int num = 0;
        //信息不能为空
        if(building.getbUnit()== null || building.getbUnit().equals("") || building.getbBuilding()==null || building.getbBuilding().equals("") ||
        building.getbRoomid()==null || building.getbRoomid().equals("") || building.getbHousetype() == null || building.getbHousetype().equals("") ||
        building.getbArea() == null || building.getbArea()<=0){
            message = "数据不能为空";
        }else {
            if(building.getbId() != null && building.getbId()!=0){
                num = buildingMapper.updateBuilding(building);
            }else {
                num = buildingMapper.saveBuilding(building);
            }
            if(num == 0){
                message="操作失败";
            }
        }
        return JSON.toJSONString(message);
    }

    @Override
    public Building findBuildingBybId(Integer bId) {
        return buildingMapper.findBuildingBybId(bId);
    }

    //删除用户
    @Override
    public int deleteBuilding(Integer bId) {
        return buildingMapper.deleteBuilding(bId);
    }
}
